package com.example.guiguiapplicatio.fragment.FamilyArchives.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.api.Interface.Api;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.FamilyarChivesRecordList;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FamilyArchivesAdapter extends RecyclerView.Adapter<FamilyArchivesAdapter.FamilyArchivesViewHolder>{
    private List<Integer> selectedPositions = new ArrayList<>();
    private int mLayoutId;
    private Context mContext;
    private Api api;

    private List<FamilyarChivesRecordList> familyarChivesRecordLists;
    public FamilyArchivesAdapter(List<FamilyarChivesRecordList> familyarChivesRecordLists, Context context,Api api) {
        this.familyarChivesRecordLists = familyarChivesRecordLists;
        mContext = context;
        this.api = api;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public FamilyArchivesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_total_detail_family_item, parent, false);
        return new FamilyArchivesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FamilyArchivesViewHolder holder, int position) {
        FamilyarChivesRecordList familyarChivesRecordList =familyarChivesRecordLists.get(position);
        holder.examinationname.setText(familyarChivesRecordList.getName());
        holder.familyMembersSex.setText(familyarChivesRecordList.getSex());
        holder.familyMembersAge.setText(Integer.toString(familyarChivesRecordList.getAge())+"岁");
        holder.familyMembersRelationship.setText(familyarChivesRecordList.getRelationship());
        switch (familyarChivesRecordList.getStatus()) {
            case 0:
                holder.examination_state.setText("待签约");
                break;
            case 1:
                holder.examination_state.setText("已签约");
                break;
            case 2:
                holder.examination_state.setText("已过期");
                break;
            default:
                holder.examination_state.setText("其他状态");
                break;
        }
        holder.familyMembersRelationship.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(familyarChivesRecordList.getRelationship().equals("户主")){
                    Toast.makeText(mContext, "不能修改户主", Toast.LENGTH_LONG).show();
                }else {
                    final String[] Tiess = new String[]{"配偶", "儿子", "女儿", "父母","祖父母","其他"};
                    final String[] Ties = new String[1];
                    int defaultPosition = 0;
                    String relationship = familyarChivesRecordList.getRelationship();
                    for (int i = 0; i < Tiess.length; i++) {
                        if (Tiess[i].equals(relationship)) {
                            defaultPosition = i;
                            break;
                        }
                    }
                    AlertDialog dialog2=new AlertDialog.Builder(mContext)
                            .setTitle("请选择成员关系")
                            //第一个参数:设置单选的资源;第二个参数:设置默认选中哪几项
                            .setSingleChoiceItems(Tiess, defaultPosition, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int i) {
                                    Ties[0] = Tiess[i];
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Call<Object> objectCall = api.updateFamilyarChives(familyarChivesRecordList.getIdcard(),Ties[0]);
                                    objectCall.clone().enqueue(new Callback<Object>() {
                                        @Override
                                        public void onResponse(Call<Object> call, Response<Object> response) {
                                            if(response.code() == 200){
                                                Toast.makeText(mContext, "修改成功，下拉刷新", Toast.LENGTH_LONG).show();;
                                            }
                                        }
                                        @Override
                                        public void onFailure(Call<Object> call, Throwable t) {

                                        }
                                    });
                                    dialog.dismiss();
                                }
                            }).create();
                    dialog2.show();
                }
            }
        });
        holder.deleteFamilyMembers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(familyarChivesRecordList.getRelationship().equals("户主")){
                    AlertDialog dialog=new AlertDialog.Builder(mContext)
                            .setTitle("删除成员")//设置标题
                            .setMessage("您删除的是户主，是否解散家庭")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();//销毁对话框
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Call<Object> objectCall = api.deleteFamilyarChives(familyarChivesRecordList.getIdcard());
                                    objectCall.clone().enqueue(new Callback<Object>() {
                                        @Override
                                        public void onResponse(Call<Object> call, Response<Object> response) {
                                            if(response.code() == 200){
                                                Toast.makeText(mContext, "删除成功，下拉刷新", Toast.LENGTH_LONG).show();
                                                notifyDataSetChanged();
                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<Object> call, Throwable t) {

                                        }
                                    });
                                    dialog.dismiss();//销毁对话框
                                }
                            }).create();
                    dialog.show();//显示对话框
                }
                else {
                    AlertDialog dialog=new AlertDialog.Builder(mContext)
                            .setTitle("删除成员")//设置标题
                            .setMessage("是否删除 "+familyarChivesRecordList.getName())
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();//销毁对话框
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Call<Object> objectCall = api.deleteFamilyarChives(familyarChivesRecordList.getIdcard());
                                    objectCall.clone().enqueue(new Callback<Object>() {
                                        @Override
                                        public void onResponse(Call<Object> call, Response<Object> response) {
                                            if(response.code() == 200){
                                                Toast.makeText(mContext, "删除成功，下拉刷新", Toast.LENGTH_LONG).show();
                                                notifyDataSetChanged();
                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<Object> call, Throwable t) {

                                        }
                                    });
                                    dialog.dismiss();//销毁对话框
                                }
                            }).create();
                    dialog.show();//显示对话框
                }
            }
        });
//        holder.examination_people_type.setText(familyarChivesRecordList.getName());
    }

    @Override
    public int getItemCount() {
        return familyarChivesRecordLists.size();
    }

    public static class FamilyArchivesViewHolder extends RecyclerView.ViewHolder {
        TextView examinationname;
        TextView familyMembersSex;
        TextView familyMembersAge;
        TextView familyMembersRelationship;
        TextView examination_state;
        TextView examinationpeople_type;

        ImageView deleteFamilyMembers;

        public FamilyArchivesViewHolder(@NonNull View itemView) {
            super(itemView);
            examinationname = itemView.findViewById(R.id.examination_name);
            familyMembersSex = itemView.findViewById(R.id.family_members_sex);
            familyMembersAge = itemView.findViewById(R.id.family_members_age);
            familyMembersRelationship = itemView.findViewById(R.id.familyMembersRelationship);
            examination_state = itemView.findViewById(R.id.examination_state);
            deleteFamilyMembers = itemView.findViewById(R.id.deleteFamilyMembers);
//            examination_people_type = itemView.findViewById(R.id.examination_people_type);
        }
    }
}
